<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="./bower_components/jquery/dist/jquery.js"></script>
    <meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
    /*function getFs(){
     var dtd  = $.Deferred();
     setTimeout(function(){
     dtd.reject({code:1});
     },2000);
     return dtd.promise();
     }

     function download(){
     var dtd  = $.Deferred();
     getFs().done(function(param){
     console.log('get Fs done code:'+ param.code);
     setTimeout(function(){
     dtd.resolve({code:2});
     },2000);
     }).fail(function(param){
     console.log('get Fs fail code:'+ param.code);
     dtd.resolve({code:-1});
     });
     return dtd.promise();
     }

     download().done(function(param){
     console.log('download done:' + param.code);
     console.log('下载完毕！');
     });*/

    /*测试pormise嵌套情况*/

    function step1() {
        console.log('before step1');
        var dtd = $.Deferred();
        setTimeout(function () {
            console.log('step1');
            dtd.resolve();
        }, 2000);
        return dtd.promise();
    }


    function step2() {
        console.log('before step2');
        var dtd = $.Deferred();
        setTimeout(function () {
            console.log('step2');
            dtd.reject(50);
        }, 2000);
        return dtd.promise();
    }

    function step3() {
        console.log('before step3');
        var dtd = $.Deferred();
        setTimeout(function () {
            console.log('step3');
            dtd.resolve();
        }, 2000);
        return dtd.promise();
    }


    /*var myqueue = $('<i/>').queue("getDir",[
      function(){
          step1().done(function(){
              myqueue.dequeue("getDir");
          });
      },step2,step3
    ]);
    debugger;
    myqueue.dequeue("getDir");*/

   var Queue = function(context){
       this.queueArray = [];
       this.queueName = "queue";
       this.queueEle = $('<i/>');
       this.args = [];
       this.context = context || this;
   };

    Queue.prototype.add = function(func,context){
        var self = this;
        this.queueArray.push(function(){
            func.apply(context || self.context,self.args).done(function(){
                self.args = arguments;
                self.exec();
            }).fail(function(){
                self.args = arguments;
                self.dtd.reject.apply(self.dtd,self.args);
            });
        });
    };

    Queue.prototype.exec = function(){
        var self = this;
        if(!this.chain){
            this.queueArray.push(function(){
                self.dtd.resolve();
                self.exec();
            });
            this.chain = this.queueEle.queue(this.queueName,this.queueArray);
            this.dtd = $.Deferred();

        }
        this.queueEle.dequeue(this.queueName);
        return this.dtd.promise();
    };


    var job = new Queue();
    job.add(step1);
    job.add(step2);
    job.add(step3);
    debugger;
    job.exec().done(function(){
        console.log('end');
    }).fail(function(errorcode){
        console.log('error' + errorcode);
    });

</script>
</body>
</html>